package com.han.shadow.customerapp.fragment;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.han.shadow.customerapp.R;
import com.han.shadow.customerapp.activity.GoodsActivity;
import com.han.shadow.customerapp.adapter.MyBusinessAdapter;
import com.han.shadow.customerapp.adapter.MyGoodsesAdapter;
import com.han.shadow.customerapp.bean.Business;
import com.han.shadow.customerapp.bean.Goods;
import com.han.shadow.customerapp.http.Connector;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

/**
 * Created by ACE on 2016/4/20 0020.
 */
public class ItemsFragment extends Fragment {

    private BaseAdapter adapter = null;
    private List<Business> businesses = new ArrayList<>();
    private ListView busList = null;
    private Connector conn = new Connector();

    private Context parent;
    private LayoutInflater inflater;
    private View view;

    public ItemsFragment(Context context,LayoutInflater inflater){
        this.parent = context;
        this.inflater = inflater;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_items,null);
        busList = (ListView)view.findViewById(R.id.itemsList);


        busList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Message msg = new Message();
                Log.i("df", "df");
                msg.what = 2;
                Bundle bundle = new Bundle();
                bundle.putString("bID", businesses.get(position).getBid());
                bundle.putString("bName",businesses.get(position).getName());
                msg.setData(bundle);
                handler.sendMessage(msg);
            }
        });


        getBusinesses();


        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    public void getBusinesses(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                businesses = conn.queryBusiness(null);
                Message msg = new Message();
                msg.what = 1;
                handler.sendMessage(msg);

            }
        }).start();
    }

    private final Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == 1){//更新商家
                if(businesses != null) {
                    adapter = new MyBusinessAdapter(businesses, inflater);
                    busList.setAdapter(adapter);
                }
            }else if(msg.what == 2){//跳转商品页面
                Intent intent = new Intent(parent,GoodsActivity.class);
                intent.putExtras(msg.getData());
                parent.startActivity(intent);
            }
        }
    };

}
